package com.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bean.ZjMusic;
import com.bean.other.MetaInfo;
import com.bean.other.RequestData;
import com.bean.other.ResponseData;
import com.bean.realation.MusicCollect;
import com.github.pagehelper.PageInfo;
import com.service.impl.ZjMusicService;
import com.utils.ResUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("api/zjmusic")
@Slf4j
public class ZjMusicController {

    @Autowired
    private ResUtil resUtil;

    @Autowired
    private ZjMusicService zjMusicService;

    //将音乐链接保存到数据库，根据 name 和 url 判断是否已经保存
    @PostMapping("add")
    public ResponseEntity addMusicFromApi(@RequestBody ZjMusic zjMusic){
        String message = zjMusicService.addMusicFromApi(zjMusic);
        return ResponseEntity.ok(resUtil.returnByMessage(message));
    }

    //播放的时候，查询数据库，根据name 和 url 判断是否已经保存，根据userid判断是否已经收藏
    //音乐状态
    @PostMapping("musicStatus")
    public ResponseEntity musicStatus(@RequestBody ZjMusic zjMusic){
        //收藏的前提需要保存
        JSONObject jsonObject =zjMusicService.musicStatus(zjMusic);
        return  ResponseEntity.ok(jsonObject);
    }

    //音乐收藏, 将音乐收藏
    @PostMapping("collect")
    public ResponseEntity collect(@RequestBody ZjMusic zjMusic){
        //收藏的前提需要保存
        String message =zjMusicService.collect(zjMusic);
        return  ResponseEntity.ok(resUtil.returnByMessage(message));
    }

    //收藏后，实现随机播放播放
    @PostMapping("sjmusic")
    public ResponseEntity randomMusic(@RequestBody ZjMusic zjMusic){
        log.info(zjMusic.toString());
        zjMusic = zjMusicService.randomMusic(zjMusic.getUserid());
        return ResponseEntity.ok(ResponseData.success(zjMusic));
    }

    @PostMapping("checkMusic")
    public ResponseEntity checkMusic(@RequestBody ZjMusic zjMusic){
        log.info(zjMusic.toString());
        List<ZjMusic> list = zjMusicService.checkMusic(zjMusic);
        return ResponseEntity.ok(ResponseData.success(list));
    }


    @PostMapping("listZjMusic")//@RequestBody只能使用一次，使用的是流，用过后就不能用了。
    public ResponseEntity<ResponseData> listZjMusic(@RequestBody RequestData<ZjMusic> requestData){
        ZjMusic zjMusic=requestData.getObj();
        MetaInfo metaInfo =requestData.getMetaInfo();
        PageInfo<ZjMusic> pageInfo=zjMusicService.select(zjMusic,metaInfo);
        metaInfo.setPages(pageInfo.getPages());
        metaInfo.setTotal((int) pageInfo.getTotal());
        return ResponseEntity.ok(ResponseData.success(pageInfo.getList(),metaInfo));
    }

    @DeleteMapping("delZjMusic/{id}")
    public ResponseEntity<ResponseData> delZjMusic(@PathVariable String id){
        zjMusicService.delZjMusic(id);
        return ResponseEntity.ok(ResponseData.success("成功"));
    }

    @PostMapping("listSelfMusic")//@RequestBody只能使用一次，使用的是流，用过后就不能用了。
    public ResponseEntity<ResponseData> listSelfMusic(@RequestBody RequestData<ZjMusic> requestData){
        ZjMusic zjMusic=requestData.getObj();
        zjMusic.setUserid(requestData.getUserid());
        MetaInfo metaInfo =requestData.getMetaInfo();
        PageInfo<ZjMusic> pageInfo=zjMusicService.listSelfMusic(zjMusic,metaInfo);
        metaInfo.setPages(pageInfo.getPages());
        metaInfo.setTotal((int) pageInfo.getTotal());
        return ResponseEntity.ok(ResponseData.success(pageInfo.getList(),metaInfo));
    }

    @PostMapping("delSelfMusic")
    public ResponseEntity<ResponseData> delSelfMusic(@RequestBody MusicCollect musicCollect){
        zjMusicService.delSelfMusic(musicCollect);
        return ResponseEntity.ok(ResponseData.success("成功"));
    }

}
